// 2013-7-29下午02:37:56  net.betou.trade.main.manager.impl.CategoryMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.core.entity.ConfigLogin;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Category;
import net.betou.trade.main.dao.CategoryDao;
import net.betou.trade.main.manager.CategoryMng;
import net.betou.trade.role.setting.CommonFinderConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class CategoryMngImpl implements CategoryMng{

	public Category deleteById( Integer id ) {
		return dao.deleteById( id );
	}

	public Category [ ] deleteByIds( Integer [ ] ids ) {
		Category [ ] beans = new Category[ ids.length ];
		
		for( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}
	@Transactional( readOnly = true )
	public Category findById( Integer id ) {
		return dao.findById( id );
	}
	@Transactional( readOnly = true )
	public Category findMyCategory( Integer id, Integer selfId, Boolean system ) {
		Category category;
		if( system ){
			category = dao.findById( id );
		}else{
			category = dao.findMyCategory( id, selfId );
		}
		
		return category;
	}
	
	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ) {
		
		Boolean system = admin.getSystem( );
		Pagination pagination;
		
		if( system ){
			pagination = dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		}else{
			
			CommonFinderConfig config = new CommonFinderConfig( admin, ConfigLogin.CONFIG_CATEGORY );
			
			if( config.isSelf( ) ){
				pagination = dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
			}else{
				pagination = dao.getPageByCriteria( searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, config.getDeepSQL( ) );
			}
		}
		
		return pagination;
	}
	
	

	public Category save( Category bean, AgentAndAdmins admin ) {
		bean.init( );
		bean.setAdmin( admin );
		return dao.save( bean );
	}

	public Category update( Category bean ) {
		bean.initUpdate( );
		Updater< Category > updater = new Updater< Category >( bean, Updater.UpdateMode.MIN ); 
		updater.include( "name" ); // 使用最小模式，只更新名称，其余全都不更新
		updater.include( "pinyin" );
		updater.include( "description" );
		return dao.updateByUpdater( updater );
	}
	
	@SuppressWarnings( "unchecked" )
	@Transactional( readOnly = true )
	public List< Map< String, String > > findListByName( AgentAndAdmins admin, String name ){
		boolean system = admin.getSystem( );
		
		List< Category > list;
		if( system ){
			list = ( List< Category >)dao.getPage( null, name, "id", "desc", 1, 10 ).getList( );
		}else{
			
			CommonFinderConfig config = new CommonFinderConfig( admin, ConfigLogin.CONFIG_CATEGORY );
			if( config.isSelf( ) ){
				list = ( List< Category >)dao.getPage( admin.getId( ), name, "id", "desc", 1, 10 ).getList( );
			}else{
				String sql = config.getDeepSQL( ) + " and ( name like '%" + name + "%' or this_.pinyin like '%" + name + "%' )";
				list = ( List< Category >) dao.getPageByCriteria( sql, "id", "desc", 1, 10 ).getList( );
			}
		}
		
		List< Map< String, String > > jsonDataList = new ArrayList< Map< String, String >>( );
		
		if( list != null && list.size( ) > 0 ){
			
			for( Category c : list ){
				Map< String, String > category = new HashMap< String, String >( );
				category.put( "name", c.getName( ) );
				category.put( "id", String.valueOf( c.getId( ) ) );
				jsonDataList.add( category );
			}
			
		}
		
		return jsonDataList;
	}
	
	@Transactional( readOnly = true )
	public boolean categoryExist( String name ){
		return dao.countByName( name ) > 0;
	}
	
	@Transactional( readOnly = true )
	public Category findByName( String name ){
		return dao.findByName( name );
	}
	
	@Autowired
	private CategoryDao dao;

}
